package org.vgou.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.directwebremoting.annotations.Param;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.vgou.dao.IBaseDao;
import org.vgou.model.Category;
import org.vgou.service.ICategoryService;

/**
 * 
 * Copyright (c) 2011, vgou <br>
 * All rights reserved.<br>
 * 
 * @since 2011-11-27
 * @author <a href="mailto:jason.hxx@gmail.com">jason</a>
 * @version Id: CategoryServiceImpl.java
 * @comment 商品类型服务类
 * 
 */
@Component("categoryService")
@RemoteProxy(name = "CategoryRemote", creator = SpringCreator.class, creatorParams = { @Param(name = "beanName", value = "CategoryServiceImpl") })
public class CategoryServiceImpl implements ICategoryService {

	protected final Log logger = LogFactory.getLog(getClass());
	@Resource
	private IBaseDao baseDao;

	/**
	 * 创建商品类型记录
	 * 
	 * @param category
	 * @return
	 * @throws Exception
	 */
	public long create(Category category) throws Exception {
		long id = 0;
		if (category == null)
			return id;
		try {
			id = (Long) baseDao.create(category);
		} catch (Exception e) {
			logger.error("创建商品类型失败：" + e.getMessage());
		}
		return id;
	}

	/**
	 * 修改商品类型记录
	 * 
	 * @param category
	 * @return
	 * @throws Exception
	 */
	public int update(Category category) throws Exception {
		int result = -1;
		if (category == null)
			return result;
		try {
			baseDao.update(category);
			result = 1;
		} catch (Exception e) {
			logger.error("修改商品类型记录失败：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 删除商品类型记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RemoteMethod
	public int del(long id) throws Exception {
		int result = -1;
		if (id <= 0)
			return result;
		try {
			Category category = getById(id);
			if (category != null)
				baseDao.delete(category);
			result = 1;
		} catch (Exception e) {
			logger.error("删除商品类型记录失败：" + e.getMessage());
		}
		return result;
	}
	
	/**
	 * 批量删除商品类型记录
	 * @param idsStr
	 * @return
	 * @throws Exception
	 */
	@RemoteMethod
	public int batchDel(String idsStr) throws Exception{
		int result = -1;
		String[] ids = idsStr.split(",");
		if(ids.length == 0)
			return result;
		try {
			for(int i=0; i<ids.length; i++){
				long id = Long.valueOf(ids[i]);
				Category category = getById(id);
				if (category != null)
					baseDao.delete(category);
			}
			result = 1;
		} catch (Exception e) {
			logger.error("批量删除商品类型记录失败：" + e.getMessage());
		}
		return result;
	}

	/**
	 * 根据商品类型ID取得商品类型记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public Category getById(long id) throws Exception {
		Category category = null;
		if (id <= 0)
			return category;
		try {
			category = (Category) baseDao.getByPk(Category.class, id);
		} catch (Exception e) {
			logger.error("根据商品类型ID取得商品类型记录失败：" + e.getMessage());
		}
		return category;
	}

	/**
	 * 取得指定条件下的商品类型数目
	 * 
	 * @param categoryName
	 *            商品类型名称
	 * @return
	 * @throws Exception
	 */
	public int getAllCount(String categoryName) throws Exception {
		int count = 0;
		Criteria c = baseDao.getCriteria(Category.class);
		if (categoryName != null && !"".equals(categoryName)) {
			c.add(Restrictions.eq("name", categoryName));
		}
		c.setProjection(Projections.rowCount());
		count = ((Number) c.uniqueResult()).intValue();
		return count;
	}

	/**
	 * 取得指定条件下的商品类型集合
	 * 
	 * @param categoryName
	 *            商品类型名称
	 * @param orderBy
	 *            排序属性
	 * @param isAsc
	 *            是否升序
	 * @param startPage
	 *            分页开始索引
	 * @param pageSize
	 *            每页显示数目
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Category> getAll(String categoryName,
			String orderBy, boolean isAsc, int startPage, int pageSize)
			throws Exception {
		List<Category> CategoryList = null;
		try {
			DetachedCriteria c = DetachedCriteria.forClass(Category.class);
			if (categoryName != null && !"".equals(categoryName)) {
				c.add(Restrictions.eq("name", categoryName));
			}
			orderBy = (orderBy == null || "".equals(orderBy)) ? "id" : orderBy;
			if (isAsc) {
				c.addOrder(Order.asc(orderBy));
			} else {
				c.addOrder(Order.desc(orderBy));
			}
			CategoryList = baseDao.findByCriteria(c, startPage, pageSize);
		} catch (Exception e) {
			logger.error("取得指定条件下的商品类型集合失败：" + e.getMessage());
		}
		return CategoryList;
	}

}
